package org.example.erat.strategy;

import org.example.erat.log.core.LoggerSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计策略工厂
 * 用于创建不同的统计策略
 */
@Component
public class StatisticsStrategyFactory {

    private final Map<String, StatisticsStrategy> strategies = new HashMap<>();

    @Autowired
    private List<StatisticsStrategy> strategyList;

    /**
     * 初始化策略
     */
    @PostConstruct
    public void init() {
        for (StatisticsStrategy strategy : strategyList) {
            strategies.put(strategy.getName(), strategy);
        }

        LoggerSingleton.getInstance().info("统计策略工厂初始化完成，支持的策略: {}",
                String.join(", ", strategies.keySet()));
    }

    /**
     * 获取策略
     * @param name 策略名称
     * @return 策略
     */
    public StatisticsStrategy getStrategy(String name) {
        StatisticsStrategy strategy = strategies.get(name);
        if (strategy == null) {
            LoggerSingleton.getInstance().warn("未找到统计策略: {}，使用默认策略: experiment", name);
            return strategies.get("experiment");
        }
        return strategy;
    }

    /**
     * 获取所有策略
     * @return 所有策略
     */
    public Map<String, StatisticsStrategy> getAllStrategies() {
        return strategies;
    }
}
